<template>
  <el-dialog
    :title="!dataForm.affiliatedCompanyId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="dataForm.companyName" placeholder="公司名称" class="width400"></el-input>
      </el-form-item>
      <el-form-item label="联络人" prop="contact">
        <el-input v-model="dataForm.contact" placeholder="联络人" class="width400"></el-input>
      </el-form-item>
      <el-form-item label="联络电话" prop="contactNumber">
        <el-input v-model="dataForm.contactNumber" placeholder="联络电话" class="width400"></el-input>
      </el-form-item>
      <el-form-item label="入网时间" prop="admissionTime">
        <!-- <el-input v-model="dataForm.admissionTime" placeholder="入网时间"></el-input> -->
        <el-date-picker v-model="dataForm.admissionTime" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="有效期截止时间" prop="expiryDate">
        <!-- <el-input v-model="dataForm.expiryDate" placeholder="有效期截止时间"></el-input> -->
        <el-date-picker v-model="dataForm.expiryDate" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="管理船员人数上线" prop="administrators">
        <el-input v-model="dataForm.administrators" placeholder="管理船员人数上线" class="width400"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <!-- <el-input v-model="dataForm.status" placeholder="状态"></el-input> -->
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item label="最后修改人" prop="updateUser">
        <el-input v-model="dataForm.updateUser" placeholder="最后修改人"></el-input>
      </el-form-item>
      <el-form-item label="最后修改时间" prop="updateDatetime">
        <el-input v-model="dataForm.updateDatetime" placeholder="最后修改时间"></el-input>
      </el-form-item>-->
    <el-form-item label="创建人" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建人" class="width400"></el-input>
    </el-form-item>
    <el-form-item label="创建人IP" prop="createUserIp">
      <el-input v-model="dataForm.createUserIp" placeholder="创建人IP" class="width400"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <!-- <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input> -->
      <el-date-picker v-model="dataForm.createTime" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
    </el-form-item>
    <el-form-item label="禁用原因" prop="orbiddenReason">
      <el-input v-model="dataForm.orbiddenReason" placeholder="禁用原因" class="width400"></el-input>
    </el-form-item>
    <el-form-item label="最后修改人IP" prop="updateUserIp">
      <el-input v-model="dataForm.updateUserIp" placeholder="最后修改人IP" class="width400"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          affiliatedCompanyId: 0,
          companyName: '',
          contact: '',
          contactNumber: '',
          admissionTime: '',
          expiryDate: '',
          administrators: '',
          status: '',
          updateUser: '',
          updateDatetime: '',
          createUserId: '',
          createUserIp: '',
          createTime: '',
          orbiddenReason: '',
          updateUserIp: ''
        },
        dataRule: {
          companyName: [
            { required: true, message: '公司名称不能为空', trigger: 'blur' },
            { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
          ],
          contact: [
            { required: true, message: '联络人不能为空', trigger: 'blur' },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          contactNumber: [
            { required: true, message: '联络电话不能为空', trigger: 'blur' },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          admissionTime: [
            { required: true, message: '入网时间不能为空', trigger: 'blur' }
          ],
          expiryDate: [
            { required: true, message: '有效期截止时间不能为空', trigger: 'blur' }
          ],
          administrators: [
            { required: true, message: '管理船员人数上线不能为空', trigger: 'blur' },
            { type: 'number', message: '必须为数字值', trigger: 'blur' },
            { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '最后修改人不能为空', trigger: 'blur' }
          ],
          updateDatetime: [
            { required: true, message: '最后修改时间不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createUserIp: [
            { required: true, message: '创建人IP不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          orbiddenReason: [
            { required: true, message: '禁用原因不能为空', trigger: 'blur' },
            { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
          ],
          updateUserIp: [
            { required: true, message: '最后修改人IP不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.affiliatedCompanyId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.affiliatedCompanyId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/affiliatedcompanyinfo/info/${this.dataForm.affiliatedCompanyId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.companyName = data.affiliatedCompanyInfo.companyName
                this.dataForm.contact = data.affiliatedCompanyInfo.contact
                this.dataForm.contactNumber = data.affiliatedCompanyInfo.contactNumber
                this.dataForm.admissionTime = data.affiliatedCompanyInfo.admissionTime
                this.dataForm.expiryDate = data.affiliatedCompanyInfo.expiryDate
                this.dataForm.administrators = data.affiliatedCompanyInfo.administrators
                this.dataForm.status = data.affiliatedCompanyInfo.status
                this.dataForm.updateUser = data.affiliatedCompanyInfo.updateUser
                this.dataForm.updateDatetime = data.affiliatedCompanyInfo.updateDatetime
                this.dataForm.createUserId = data.affiliatedCompanyInfo.createUserId
                this.dataForm.createUserIp = data.affiliatedCompanyInfo.createUserIp
                this.dataForm.createTime = data.affiliatedCompanyInfo.createTime
                this.dataForm.orbiddenReason = data.affiliatedCompanyInfo.orbiddenReason
                this.dataForm.updateUserIp = data.affiliatedCompanyInfo.updateUserIp
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/affiliatedcompanyinfo/${!this.dataForm.affiliatedCompanyId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'affiliatedCompanyId': this.dataForm.affiliatedCompanyId || undefined,
                'companyName': this.dataForm.companyName,
                'contact': this.dataForm.contact,
                'contactNumber': this.dataForm.contactNumber,
                'admissionTime': this.dataForm.admissionTime,
                'expiryDate': this.dataForm.expiryDate,
                'administrators': this.dataForm.administrators,
                'status': this.dataForm.status,
                'updateUser': this.dataForm.updateUser,
                'updateDatetime': this.dataForm.updateDatetime,
                'createUserId': this.dataForm.createUserId,
                'createUserIp': this.dataForm.createUserIp,
                'createTime': this.dataForm.createTime,
                'orbiddenReason': this.dataForm.orbiddenReason,
                'updateUserIp': this.dataForm.updateUserIp
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
